package com.pinguix.games.pong;

import danger.ui.Rect;

public class PongAI
{
	public static final int VERYHARD = 4;	//Calculates and anticipates angles perfectly
	public static final int HARD 	= 3;	//Anticipates where ball will go but sometimes erroneously
	public static final int MEDIUM	= 2;	//Will attempt to match the ball position 1-1 at all times
	public static final int EASY 	= 1;	//Moves up and down and tries to catch ball after 3/4 point
	public static final int VERYEASY = 1;	//Moves up and down randomly
	
	private int _difficulty = 1;
	private boolean _isActive = true;
	
	private Paddle _paddle;
	private Rect _ballBounds;
	private Rect _paddleBounds;
	
	public PongAI(Paddle host, Ball ball)
	{
		_paddle = host;
		_paddleBounds = host.getBounds();
		_ballBounds = ball.getBounds();
	}
	
	public void update()
	{
		if(_isActive)
		{
			switch(_difficulty)
			{
				case 0:
				case 2:
				case 3:
				case 1:
					//If barY is above ballY, move down, else, move up
					if(vertDistanceCenter(10) < 0)
					{
						_paddle.moveDownConstant();
					}
					else if(vertDistanceCenter(10) > 0)
					{
						_paddle.moveUpConstant();
					}
					else
					{
						_paddle.stopMoving();
					}
					break;
			}
		}
	}

	private int vertDistance()
	{
		if(_paddleBounds.top <= _ballBounds.top && _paddleBounds.bottom >= _ballBounds.bottom)
			return 0;
		else if(_paddleBounds.top > _ballBounds.bottom)
			return _ballBounds.bottom - _paddleBounds.top;
		else if(_paddleBounds.bottom < _ballBounds.top)
			return _ballBounds.top - _paddleBounds.bottom;
		
		return 0;
	}
	private int vertDistanceCenter(int centerCushion)
	{
		if(centerCushion != 0 &&_ballBounds.top + _ballBounds.getHeight()/2 > 
				_paddleBounds.top + _paddleBounds.getHeight()/2 - centerCushion && 
				_ballBounds.top + _ballBounds.getHeight()/2 < 
				_paddleBounds.bottom - _paddleBounds.getHeight()/2 + centerCushion)
			return 0;
		return(_paddleBounds.top + _paddleBounds.getHeight()/2 - _ballBounds.top + _ballBounds.getHeight()/2);
	}
	
	public void activate()
	{
		_isActive = true;
	}
	public void deactivate()
	{
		_isActive = false;
	}
	public boolean isActive()
	{
		return _isActive;
	}
	
	public void changeDifficulty(int difficulty)
	{
		_difficulty = difficulty;
	}
	
	public void setPaddle(Paddle host)
	{
		_paddle = host;
	}
}
